using System;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;

namespace PublishingImageWebPart
{
    public class AnchorTargetEditorPart : EditorPart
    {
        private DropDownList dropdown = null;
        private TextBox textbox = null;

        #region TargetValueProperty
        private string _TargetValueProperty = null;

        public string TargetValueProperty
        {
            get { return _TargetValueProperty; }
            set { _TargetValueProperty = value; }
        }
        #endregion
        
        #region LabelText
        private string _LabelText = null;

        public string LabelText
        {
            get { return _LabelText; }
            set { _LabelText = value; }
        }
        #endregion

        #region ToolTipText
        private string _ToolTipText = null;

        public string ToolTipText
        {
            get { return _ToolTipText; }
            set { _ToolTipText = value; }
        }
        #endregion

        protected string TargetValue
        {
            get
            {
                System.Reflection.PropertyInfo propertyinfo = WebPartToEdit.GetType().GetProperty(TargetValueProperty);
                if (propertyinfo == null) throw new ArgumentOutOfRangeException("TargetValueProperty");

                object value = propertyinfo.GetValue(WebPartToEdit, null);
                return value != null ? value.ToString() : null;
            }
            set
            {
                System.Reflection.PropertyInfo propertyinfo = WebPartToEdit.GetType().GetProperty(TargetValueProperty);
                if (propertyinfo == null) throw new ArgumentOutOfRangeException("TargetValueProperty");

                propertyinfo.SetValue(WebPartToEdit, value, null);
            }
        }

        /// <summary>
        /// Creates the control cell.
        /// </summary>
        /// <param name="control">The control.</param>
        /// <param name="labelText">The label text.</param>
        /// <param name="toolTipText">The tool tip text.</param>
        /// <returns>Formatted TableCell</returns>
        private static TableCell CreateControlCell(Control control, string labelText, string toolTipText)
        {
            TableCell cell = new TableCell();

            Panel panelHeader = new Panel();
            cell.Controls.Add(panelHeader);
            panelHeader.CssClass = "UserSectionHead";

            Label label = new Label();
            panelHeader.Controls.Add(label);
            label.Text = labelText;
            label.AssociatedControlID = control.ID;
            if (!string.IsNullOrEmpty(toolTipText))
            {
                label.ToolTip = toolTipText;
            }

            Panel panelBody = new Panel();
            cell.Controls.Add(panelBody);
            panelBody.CssClass = "UserSectionBody";

            Panel panelGroup = new Panel();
            panelBody.Controls.Add(panelGroup);
            panelGroup.CssClass = "UserControlGroup";
            panelGroup.Controls.Add(control);

            Panel panelLine = new Panel();
            cell.Controls.Add(panelLine);
            panelLine.CssClass = "UserDottedLine";

            return cell;
        }

        private TableRow CreateTargetSection()
        {            
            dropdown = new DropDownList();
            dropdown.SelectedIndexChanged += new EventHandler(dropdown_SelectedIndexChanged);

            dropdown.Items.Add(new ListItem("New window" , "_blank"));
            dropdown.Items.Add(new ListItem("Parent" , "_parent"));
            dropdown.Items.Add(new ListItem("Self" , "_self"));
            dropdown.Items.Add(new ListItem("Top most frame" , "_top"));
            dropdown.Items.Add(new ListItem("Named window", "Custom"));

            TableRow row = new TableRow();

            row.Cells.Add(CreateControlCell(dropdown, "Hyperlink Target:", null));

            return row;
        }

        private TableRow CreateWindowNameSection()
        {
            textbox = new TextBox();

            TableRow row = new TableRow();

            row.Cells.Add(CreateControlCell(textbox, "Window name:", null));

            return row;
        }


        void dropdown_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dropdown != null && textbox != null)
            {
                if (string.Compare(dropdown.SelectedValue, "Custom", true) == 0)
                {
                    TargetValue = textbox.Text;
                }
                else
                {
                    TargetValue = dropdown.SelectedValue;
                }
            }
        }

        protected override void CreateChildControls()
        {
            Table table = new Table();
            Controls.Add(table);
            table.Width = Unit.Percentage(100);
            table.CellPadding = 0;
            table.CellSpacing = 0;
            table.BorderWidth = 0;

            TableRow row1 = CreateTargetSection();
            table.Rows.Add(row1);

            TableRow row2 = CreateWindowNameSection();
            table.Rows.Add(row2);
        }

        public override void SyncChanges()
        {
            EnsureChildControls();
            if (textbox != null && dropdown != null)
            {
                ListItem item = dropdown.Items.FindByValue(TargetValue);
                if (item != null)
                {
                    item.Selected = true;
                }
                else
                {
                    item = dropdown.Items.FindByValue("Custom");
                    if (item != null)
                    {
                        item.Selected = true;
                        textbox.Text = TargetValue;
                    }
                }
            }
        }


        public override bool ApplyChanges()
        {
            if (textbox != null && dropdown!=null)
            {
                if (string.Compare(dropdown.SelectedValue, "Custom", true) == 0)
                {
                    TargetValue = textbox.Text;
                }
                else
                {
                    TargetValue = dropdown.SelectedValue;
                }
            }
            else return false;
            return true;
        }

        public AnchorTargetEditorPart(string TargetValueProperty, string title)
        {
            this.TargetValueProperty = TargetValueProperty;
            this.Title = title;
        }

    }
}
